package com.telit.firecommand.controller;

import com.alibaba.fastjson.JSON;
import com.telit.common.constant.Constant;
import com.telit.common.dto.DistrictDTO;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.common.util.RedisUtils;
import com.telit.firecommand.constant.RedisKeyConstant;
import com.telit.firecommand.entity.HourlyWeather;
import com.telit.firecommand.entity.NowWeather;
import com.telit.firecommand.model.cityVisualization.*;
import com.telit.firecommand.service.CityVisualizationService;
import com.telit.firecommand.service.DistrictService;
import com.telit.firecommand.service.WeatherService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @Author:zhangwei
 * @Description:
 * @Time:Created on 2023/3/13 17:02
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/city-visualization", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "市级可视化接口", value = "市级可视化接口", produces = MediaType.APPLICATION_JSON_VALUE)
public class CityVisualizationController {

    private final WeatherService weatherService;

    private final DistrictService districtService;

    private final CityVisualizationService cityVisualizationService;

    @GetMapping(value = "/nowWeather", produces = "application/json;charset=UTF-8")
    @ApiOperation("获取实时天气信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "districtCode", value = "行政区划码（例如：420502）", paramType = "query", dataType = "String"),
    })
    public DataReturn<NowWeather> nowWeather(@RequestParam("districtCode") String districtCode) {
        NowWeather nowWeather;
        String nowWeatherJson = RedisUtils.get(RedisKeyConstant.NOW_WEATHER + districtCode);
        if (StringUtils.isNotEmpty(nowWeatherJson)) {
            nowWeather = JSON.parseObject(nowWeatherJson, NowWeather.class);
        } else {
            Long expireTime = (long) (0.5 * 60 * 60);
            DistrictDTO districtInfo = districtService.qryDistrictInfo(districtCode);
            nowWeather = weatherService.getNowWeather(districtInfo.getName());
            RedisUtils.set(RedisKeyConstant.NOW_WEATHER + districtCode, JSON.toJSONString(nowWeather), expireTime);
        }
        return DataReturn.ok(nowWeather);
    }

    @GetMapping(value = "/hourlyWeather", produces = "application/json;charset=UTF-8")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "districtCode", value = "行政区划码（例如：420502）", paramType = "query", dataType = "String"),
    })
    @ApiOperation("24小时逐小时天气预报")
    public DataReturn<List<HourlyWeather>> getHourlyWeather(@RequestParam("districtCode") String districtCode) {
        List<HourlyWeather> hourlyWeatherList;
        String hourlyWeatherJson = RedisUtils.get(RedisKeyConstant.HOURLY_WEATHER + districtCode);
        if (StringUtils.isNotEmpty(hourlyWeatherJson)) {
            hourlyWeatherList = JSON.parseArray(hourlyWeatherJson, HourlyWeather.class);
        } else {
            Long expireTime = (long) (0.5 * 60 * 60);
            DistrictDTO districtInfo = districtService.qryDistrictInfo(districtCode);
            hourlyWeatherList = weatherService.getHourlyWeather(districtInfo.getName());
            RedisUtils.set(RedisKeyConstant.HOURLY_WEATHER + districtCode, JSON.toJSONString(hourlyWeatherList),
                    expireTime);
        }

        return DataReturn.ok(hourlyWeatherList);
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "火情统计")
    @GetMapping(path = "/qry-fire-count")
    public DataReturn<QryFireCountModel> qryFireCount(QryFireCountRequest qryFireCountRequest) {
        return DataReturn.ok(cityVisualizationService.qryFireCount(qryFireCountRequest));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "火情趋势")
    @GetMapping(path = "/qry-fire-trend-30day")
    public DataReturn<List<QryFireTrend30DayModel>> qryFireTrend30Day(
            QryFireTrend30DayRequest qryFireTrend30DayRequest) {
        return DataReturn.ok(cityVisualizationService.qryFireTrend30Day(qryFireTrend30DayRequest));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "火灾统计")
    @GetMapping(path = "/qry-fire-trend-1year")
    public DataReturn<List<Map<String, List<QryFireTrend1YearResult.LevelCount>>>> qryFireTrend1Year(
            QryFireTrend1YearRequest qryFireTrend1YearRequest) {
        return DataReturn.ok(cityVisualizationService.qryFireTrend1Year(qryFireTrend1YearRequest));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "火情列表")
    @GetMapping(path = "/qry-fire-list")
    public DataReturn<PageData<List<QryFireListModel>>> qryFireList(QryFireListRequest qryFireListRequest) {
        return DataReturn.ok(cityVisualizationService.qryFireList(qryFireListRequest));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "火情详情")
    @GetMapping(path = "/qry-fire-detail/{id}")
    public DataReturn<QryFireDetailModel> qryFireDetail(@PathVariable(name = "id") Long id) {
        return DataReturn.ok(cityVisualizationService.qryFireDetail(id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "fireId", value = "火情id(查询对应火情的报警监控实时播放地址)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "cameraId", value = "视频监控id（优先级高于fireId，查询指定监控的实时播放流地址）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "返回流类型：1:rtsp 2:http-flv 默认 1", paramType = "query", dataType = "String"),
    })
    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "实时视频流")
    @GetMapping(path = "/qry-camera-url")
    public DataReturn<QryCameraUrlModel> qryCameraUrl(@RequestParam(value = "fireId", required = false) Long fireId,
                                                      @RequestParam(value = "cameraId", required = false) String cameraId,
                                                      @RequestParam(value = "type", required = false, defaultValue = "1") Integer type) {
        return DataReturn.ok(cityVisualizationService.qryCameraUrl(fireId, cameraId, type));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "历史视频流")
    @GetMapping(path = "/qry-camera-history-url")
    public DataReturn<QryCameraHistoryUrlModel> qryCameraHistoryUrl(
            QryCameraHistoryUrlRequest qryCameraHistoryUrlRequest) {
        return DataReturn.ok(cityVisualizationService.qryCameraHistoryUrl(qryCameraHistoryUrlRequest));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "物资统计")
    @GetMapping(path = "/qry-resource-count")
    public DataReturn<List<QryResourceCountModel>> qryResourceCount(QryResourceCountRequest qryResourceCountRequest) {
        return DataReturn.ok(cityVisualizationService.qryResourceCount(qryResourceCountRequest));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "物资分类")
    @GetMapping(path = "/qry-resource-cat")
    public DataReturn<List<QryResourceCatModel>> qryResourceCat(QryResourceCatRequest qryResourceCatRequest) {
        return DataReturn.ok(cityVisualizationService.qryResourceCat(qryResourceCatRequest));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "设施分类")
    @GetMapping(path = "/qry-equipment-cat")
    public DataReturn<List<QryEquipmentCatModel>> qryEquipmentCat(QryEquipmentCatRequest qryEquipmentCatRequest) {
        return DataReturn.ok(cityVisualizationService.qryEquipmentCat(qryEquipmentCatRequest));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "物资列表")
    @GetMapping(path = "/qry-resource-list")
    public DataReturn<PageData<List<QryResourceListModel>>> qryResourceList(
            QryResourceListRequest qryResourceListRequest) {
        return DataReturn.ok(cityVisualizationService.qryResourceList(qryResourceListRequest));
    }

//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "tableId", value = "表id", paramType = "query", dataType = "String"),
//            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String"),
//    })
//    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "物资详情")
//    @GetMapping(path = "/qry-resource-detail/{tableId}/{id}")
//    public DataReturn<QryResourceDetailModel> qryResourceDetail(@PathVariable(value = "tableId") String tableId,
//                                                                @PathVariable(value = "id") String id) {
//        return DataReturn.ok(cityVisualizationService.qryResourceDetail(tableId, id));
//    }

    @ApiResponses({
            @ApiResponse(code = 200, message = "data 为数据实体类，metaField 为数据实体类字段的描述")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", value = "表id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String"),
    })
    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "物资详情")
    @GetMapping(path = "/qry-resource-detail/{tableId}/{id}")
    public DataReturn<Map<String, Object>> qryResourceDetail(@PathVariable(value = "tableId") String tableId,
                                                             @PathVariable(value = "id") String id) {
        return DataReturn.ok(cityVisualizationService.qryResourceDetail(tableId, id));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "设施统计")
    @GetMapping(path = "/qry-resource-equipment-count")
    public DataReturn<List<QryResourceEquipmentCountModel>> qryResourceEquipmentCount(
            QryResourceEquipmentCountRequest qryResourceEquipmentCountRequest) {
        return DataReturn.ok(cityVisualizationService.qryResourceEquipmentCount(qryResourceEquipmentCountRequest));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "设施列表")
    @GetMapping(path = "/qry-resource-equipment-list")
    public DataReturn<PageData<List<QryResourceEquipmentListModel>>> qryResourceEquipmentList(
            QryResourceEquipmentListRequest qryResourceEquipmentListRequest) {
        return DataReturn.ok(cityVisualizationService.qryResourceEquipmentList(qryResourceEquipmentListRequest));
    }

    @ApiResponses({
            @ApiResponse(code = 200, message = "data 为数据实体类，metaField 为数据实体类字段的描述")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", value = "表id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String"),
    })
    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "设施详情")
    @GetMapping(path = "/qry-resource-equipment-detail/{tableId}/{id}")
    public DataReturn<Map<String, Object>> qryResourceEquipmentDetail(
            @PathVariable(value = "tableId") String tableId, @PathVariable(value = "id") String id) {
        return DataReturn.ok(cityVisualizationService.qryResourceEquipmentDetail(tableId, id));
    }

//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "tableId", value = "表id", paramType = "query", dataType = "String"),
//            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String"),
//    })
//    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "设施详情")
//    @GetMapping(path = "/qry-resource-equipment-detail/{tableId}/{id}")
//    public DataReturn<QryResourceEquipmentDetailModel> qryResourceEquipmentDetail(
//            @PathVariable(value = "tableId") String tableId, @PathVariable(value = "id") String id) {
//        return DataReturn.ok(cityVisualizationService.qryResourceEquipmentDetail(tableId, id));
//    }

}
